from pyftpdlib import ftpserver
import os
import sys

def myftp_server(ip, port, usr_data=[], folder_dft=''):
	#folder="H:\\myftp"
	if not folder_dft:
		#folder = os.getcwd()
		user_folder = os.name == 'nt' and os.getenv("userprofile") or os.getenv("HOME")
		folder_dft = os.path.join(user_folder,'myftp')
	authorizer = ftpserver.DummyAuthorizer()
	for usr in usr_data:
		folder = usr.get('foder', folder_dft) or folder_dft
		if not os.path.exists( folder ):
			os.makedirs(folder)
		authorizer.add_user(usr['usr'], usr['pwd'], folder, usr['perm'])
	authorizer.add_anonymous(folder_dft)
	ftp_handler = ftpserver.FTPHandler
	ftp_handler.authorizer = authorizer
	address = (ip, port)
	ftpd = ftpserver.FTPServer(address, ftp_handler)
	ftpd.serve_forever()
	
if __name__ == '__main__':
	ip = sys.argv[1]
	port = sys.argv[2]
	usr_data = eval( open('ftpserver_usrdata.dict.py', 'r').read() )
	try:
		usr_data = eval( open('ftpserver_usrdata.dict.py', 'r').read() )
	except:
		#usr_data=[{'usr': 'moylop260', 'pwd': 'moylop260', 'folder': '', 'perm': 'elradfmw'}]
		print "error"
		pass
	#folder_dft = sys.argv[3]
	#usr_data = sys.argv[4]
	myftp_server(ip, port, usr_data=usr_data)
	# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
